#within Donor simulation script

#Go to Appropriate directory
library(stringr)
library(fastLink)
library(tidyverse)
rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()

here::i_am("Scripts/withindonorsims.R")

library(here)

df<-readRDS(here("Data","withinanalysisset.rds"))



dc_sd<-unique(df$finstate3dc)

df<-df %>%
  group_by(finstate3dc) %>%
  mutate(rid=row_number())
df$dc_rid<-paste(df$finstate3dc,df$rid,sep="_")

df2<-df[,which(colnames(df)%in%c("rcAMOUNT","finstate3dc","recipient.cfscore","fundraiser", "rid", "dc_rid"))]



numsims<-200

simcols<-as.data.frame(matrix(0,nrow=nrow(df2),ncol=numsims))
colnames(simcols)<-paste("fundraiser_s",seq(1,numsims,1),sep="")
simcols$dc_rid<-df2$dc_rid
df2<-merge(df2,simcols,by=c("dc_rid"))

#set.seed
set.seed(81)


#first iteration
frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))

df2$fundraiser_s1[which(df2$dc_rid%in%frp==T)]<-1

#next 199 iterations

for(i in 2:numsims){
  print(i)
  frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))
  df2[which(df2$dc_rid%in%frp==T),c(7+i)]<-1
}

#saveRDS(df2, "withindonorsimdf1.rds")

df2<-df[,which(colnames(df)%in%c("rcAMOUNT","finstate3dc","recipient.cfscore","fundraiser", "rid", "dc_rid"))]



numsims<-200

simcols<-as.data.frame(matrix(0,nrow=nrow(df2),ncol=numsims))
colnames(simcols)<-paste("fundraiser_s",seq(201,400,1),sep="")
simcols$dc_rid<-df2$dc_rid
df2<-merge(df2,simcols,by=c("dc_rid"))

#set.seed
set.seed(82)


#first iteration
frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))

df2$fundraiser_s201[which(df2$dc_rid%in%frp==T)]<-1

#next 199 iterations

for(i in 2:numsims){
  print(i)
  frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))
  df2[which(df2$dc_rid%in%frp==T),c(6+i)]<-1
}

#saveRDS(df2, "withindonorsimdf201.rds")

df2<-df[,which(colnames(df)%in%c("rcAMOUNT","finstate3dc","recipient.cfscore","fundraiser", "rid", "dc_rid"))]



numsims<-200

simcols<-as.data.frame(matrix(0,nrow=nrow(df2),ncol=numsims))
colnames(simcols)<-paste("fundraiser_s",seq(401,600,1),sep="")
simcols$dc_rid<-df2$dc_rid
df2<-merge(df2,simcols,by=c("dc_rid"))

#set.seed
set.seed(83)


#first iteration
frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))

df2$fundraiser_s401[which(df2$dc_rid%in%frp==T)]<-1

#next 199 iterations

for(i in 2:numsims){
  print(i)
  frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))
  df2[which(df2$dc_rid%in%frp==T),c(7+i)]<-1
}

#saveRDS(df2, "withindonorsimdf401.rds")

df2<-df[,which(colnames(df)%in%c("rcAMOUNT","finstate3dc","recipient.cfscore","fundraiser", "rid", "dc_rid"))]



numsims<-200

simcols<-as.data.frame(matrix(0,nrow=nrow(df2),ncol=numsims))
colnames(simcols)<-paste("fundraiser_s",seq(601,800,1),sep="")
simcols$dc_rid<-df2$dc_rid
df2<-merge(df2,simcols,by=c("dc_rid"))

#set.seed
set.seed(84)


#first iteration
frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))

df2$fundraiser_s601[which(df2$dc_rid%in%frp==T)]<-1

#next 199 iterations

for(i in 2:numsims){
  print(i)
  frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))
  df2[which(df2$dc_rid%in%frp==T),c(7+i)]<-1
}

#saveRDS(df2, "withindonorsimdf601.rds")


df2<-df[,which(colnames(df)%in%c("rcAMOUNT","finstate3dc","recipient.cfscore","fundraiser", "rid", "dc_rid"))]



numsims<-200

simcols<-as.data.frame(matrix(0,nrow=nrow(df2),ncol=numsims))
colnames(simcols)<-paste("fundraiser_s",seq(801,1000,1),sep="")
simcols$dc_rid<-df2$dc_rid
df2<-merge(df2,simcols,by=c("dc_rid"))

#set.seed
set.seed(85)


#first iteration
frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))

df2$fundraiser_s801[which(df2$dc_rid%in%frp==T)]<-1

#next 499 iterations

for(i in 2:numsims){
  print(i)
  frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))
  df2[which(df2$dc_rid%in%frp==T),c(6+i)]<-1
}

#saveRDS(df2, "withindonorsimdf801.rds")

df2<-df[,which(colnames(df)%in%c("rcAMOUNT","finstate3dc","recipient.cfscore","fundraiser", "rid", "dc_rid"))]



numsims<-200

simcols<-as.data.frame(matrix(0,nrow=nrow(df2),ncol=numsims))
colnames(simcols)<-paste("fundraiser_s",seq(1001,1200,1),sep="")
simcols$dc_rid<-df2$dc_rid
df2<-merge(df2,simcols,by=c("dc_rid"))

#set.seed
set.seed(86)


#first iteration
frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))

df2$fundraiser_s1001[which(df2$dc_rid%in%frp==T)]<-1

#next 199 iterations

for(i in 2:numsims){
  print(i)
  frp<-unlist(lapply(dc_sd,function(x) paste(x,sample(seq(1,length(which(df2$finstate3dc==x)),1),sum(df2$fundraiser[df2$finstate3dc==x])),sep="_")))
  df2[which(df2$dc_rid%in%frp==T),c(6+i)]<-1
}

#saveRDS(df2, "withindonorsimdf1001.rds")

